<?php
session_start();
require_once('config.php');
function __autoload($class) {
    global $classFolder;
    require_once($classFolder. $class. ".class.php");
}

//DB object
global $db;
$db = new db($mysqlSettings);
$db->enableErrors();

//Create a box if not created!
$userID = $_SESSION['userID'];
$getUserId = $_POST['userid'];

if(empty($userID)) {
    $genUserId = randomGen();
    $userID = $genUserId;
    $insert = $db->insert('boxes', array('', $genUserId, 200, 1));
    if($insert) {
        $_SESSION['userID'] = $genUserId;
    }
} 


//Retrives post from move buttons    
if($_POST['bot']) {
    $but = $_POST['bot'];
    $db->connect(false);
    $db->update('boxes', array("json" => $but, "new_data" => 1), "userid=$userID");
    $db->disconnect();
}


//Retrives message for close event
if($_POST['event'] == "close") {
    $db->connect(false);
    if($db->update('boxes', array("json" => 300, "new_data" => 1), "userid=$userID")) {
        session_unset();
        session_destroy();
    }
    $db->disconnect();
}

?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>Phone</title>
        <link  type="text/css" rel="stylesheet" href="css.css"/>
        <script type="text/javascript" src="jquery-1.4.4.min.js"></script>
        <script type="text/javascript">
        //1 up, 2 left, 3 down, 4 right
                
            var UserId = <?php echo $userID; ?>
            
            $('document').ready(function() {
                $('#up').click(function() {
                    sendEvent(1);
                })
                    
                $('#down').click(function() {
                    sendEvent(3);
                })
                
                $('#left').click(function() {
                    sendEvent(2);
                })
                
                $('#right').click(function() {
                    sendEvent(4);
                })
                
                function sendEvent(action) {
                    $('#buttens').hide();
                    $('#updating').show();
                    $.ajax({
                        type: "POST",
                        url: "phone.php",
                        data: "bot=" + action,
                        success: function(msg) {
                            $('#buttens').show();
                            $('#updating').hide();
                        }
                    })
                }
                
                $('#close').click(function() {
                    $.ajax({
                        type: "POST",
                        url: "phone.php",
                        data: "event=close",
                        success: function(msg) {
                            alert("Yor box is deleted!");
                            window.location = "index.php";
                        }
                    });
                })
            })
        </script>
    </head>
    <body>
        <div id="top">
            <span>:::::The box game v0.3:::</span>
        </div>
        <div id="buttens">
        <div id="updating">Please wait...</div>
        <div id="userId">Your UserID: <?php echo $userID ?></div>
        <button id="up">Up</button>
        <button id="down">Down</button><br />
        <button id="left">Left</button>
        <button id="right">Right</button>
        <button id="close">Delete Box</button>
        <div id="event"></div>
        </div>
    </body>
</html>

<?php 
    function randomGen() {
        global $db;
    
        //$genUserId = 1;
        $table = $db->getTable('boxes');
        $db->connect(false);
        for(;;) {
            $genUserId = rand(0000,9999);
            $data = $db->get("SELECT * FROM $table WHERE userid='$genUserId'");
            
            if(!is_array($data)) {
                    return $genUserId;
            }
        }
        $db->disconnect();
    } 
?>